package ug.co.icom.kyazze.healthticket.mongoHQ;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Locale;







import ug.co.icom.kyazze.healthticket.data.QueryBuilder;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

public class ObjectIDSearch {

	static BasicDBObject user = null;
	static String OriginalObject = "";
	static String server_output = null;
	static String temp_output = null;
	static Object object_id = "";
	@SuppressWarnings("unused")
	private Locale locale = Locale.getDefault();

	public Object getObjectID(String randomtag) {
		try {
			QueryBuilder qb = new QueryBuilder();
			URL url = new URL(qb.buildMedicalObjectUrl(randomtag));
			HttpURLConnection conn = (HttpURLConnection) url
					.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Accept", "application/json");

			if (conn.getResponseCode() > 205) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			//System.out.println("Output from Server .... \n");
			while ((temp_output = br.readLine()) != null) {
				server_output = temp_output;
			}
			
			String val = server_output;
			@SuppressWarnings("unused")
			String val2 = val;

			// Remove square brackets
			OriginalObject = server_output.substring(1, server_output.length() - 1);

			Object o = com.mongodb.util.JSON.parse(OriginalObject);

			DBObject userObj = (DBObject) o;
			
			//Dummy temp = new Dummy();

			object_id = userObj.get("_id");
			
			Object desired = object_id;
			@SuppressWarnings("unused")
			Object confirm = desired;

		}catch (Exception e) {
			e.printStackTrace();
		}

		return object_id;
	}
	

}
